import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
/*
createRouter创建路由实例
配置history模式
history模式createWebHistory地址栏不带#
hash模式createWebHashHistory 地址栏带#
*/
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //登陆架子
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    //主页面架子
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/proof/indexStart', //重定向到指定页面
      children: [
        //表示子页面的定向
        {
          path: '/proof/indexStart',
          component: () => import('@/views/Proof/ProofStudentIndex.vue'),
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue'),
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue'),
        },
        {
          path: '/user/register',
          component: () => import('@/views/user/UserRegister.vue'),
        },
        {
          path: '/project',
          component: () => import('@/views/project/ProjectView.vue'),
        },
        {
          path: '/manage',
          component: () => import('@/views/user/UserManagement.vue'),
        },
      ],
    },
  ],
})

// 登陆拦截访问  to即将要进入的目标,from当前导航要离开的目标
//如果默认不写或者返回true表示放行  返回false表示不放行  返回路由地址表示跳转到指定的地址
router.beforeEach((to) => {
  //如果没有token  表示没有登陆  跳转到登陆页
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})

export default router
